<?php

namespace IMT\DashboardBundle\Controller;

use IMT\lugins\ParticipantsBundle\Entity\ParticipantEvent;

use IMT\AccountBundle\Entity\AccountGmail;
use IMT\AccountBundle\Entity\AccountIMT;
use IMT\UserBundle\Entity\Contact;
use IMT\EventBundle\Entity\Event;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;


class DefaultController extends Controller
{
	public function welcomeAction(){
		return $this->redirect($this->generateUrl('accueil_planning'));
	}
    
    public function indexAction()
    {
    	$emEvent = $this->container->get('imt_event.event_manager');
    	$securityContext = $this->get('security.context');
    	$user = $securityContext->getToken()->getUser();
    	    	
    	$em = $this->getDoctrine()->getEntityManager();
    	
        return $this->render('IMTDashboardBundle:Default:index.html.twig');
    }
    
    public function headerAction()
    {
    	$emEvent = $this->container->get('imt_event.event_manager');
    	$securityContext = $this->get('security.context');
    	$user = $securityContext->getToken()->getUser();
    	
    	$em = $this->getDoctrine()->getEntityManager();
    	$repoPart = $em->getRepository('IMTPluginsParticipantsBundle:ParticipantEvent');
    	
    	$profils = $user->getProfils();
    	$notifications = array();
    	
    	$accounts = $this->getDoctrine()->getRepository('IMTAccountBundle:Account')->findByActiveAndUser(true, $user);
    	
    	foreach($accounts as $account){
    		foreach($account->getParticipantsEvent() as $participation){
    			$event = $participation->getEvent();
    			$owners = $event->getOwners();
    			 
    			if($user->getId() != $owners[0]->getId() && !$participation->getViewed()){
    				$notifications[] = $participation;
    			}
    		}
    	}
    	
    	return $this->render('IMTDashboardBundle::header.html.twig', array(
    		'notifications' => $notifications
    	));
    }
    
    public function redirectToEventAction($idPart)
    {
    	$emEvent = $this->container->get('imt_event.event_manager');
    	$securityContext = $this->get('security.context');
    	$user = $securityContext->getToken()->getUser();
    	 
    	$em = $this->getDoctrine()->getEntityManager();
    	$repoPart = $em->getRepository('IMTPluginsParticipantsBundle:ParticipantEvent');
    	
    	$part = $repoPart->find($idPart);
    	if(!$part){
    		throw $this->createNotFoundException('L\'événement demandé est introuvable');
    	} 
    	
    	$part->setViewed(true);
    	
    	$em->persist($part);
    	$em->flush();
    	
    	return $this->redirect($this->generateUrl($part->getEvent()->getType().'_homepage', array('id' => $part->getEvent()->getId())));
    }
}
